#!/bin/bash
############################################
if [[ $# > 0 && "$1" != "-"* ]]; then
  PRJ=$1
else 
  PRJ="test"
fi

PRJ=${PRJ%%/*}

if [[ -d $PRJ ]]; then
  read -p "Caution: directory [$PRJ] exsit, continue? (y/n): " input

  if [[ ${input,,} == *"y"* ]]; then
    rm -rf $PRJ
  else
    exit 1
  fi
fi

mkdir -p $PRJ/project

echo "name := \"$PRJ\"" >> $PRJ/build.sbt
echo "version := \"1.0\"" >> $PRJ/build.sbt
echo "scalaVersion := \"2.11.12\"" >> $PRJ/build.sbt
echo "organization := \"com.$PRJ\"" >> $PRJ/build.sbt
echo "" >> $PRJ/build.sbt

echo "lazy val $PRJ = (project in file(\".\"))
  .settings(
    libraryDependencies ++= Seq(\"org.scalatest\" % \"scalatest_2.11\" % \"2.2.1\",
	\"org.yaml\" % \"snakeyaml\" % \"1.8\"
	)
   )" >> $PRJ/build.sbt

echo "sbt.version = 1.4.5" > $PRJ/project/build.properties

mkdir -p $PRJ/src/main/scala
mkdir -p $PRJ/src/main/resources
mkdir -p $PRJ/src/test/scala
mkdir -p $PRJ/src/test/resources

echo "package $PRJ

object $PRJ {
  def main(args: Array[String]): Unit = {
    println(\"Hello the world!\")
  }
}" > $PRJ/src/main/scala/$PRJ.scala

echo "project: $PRJ initilized"	

[[ "$*" == *"-run"* ]] && echo "Try to run.." && cd $PRJ && sbt run
